Imports System.Text.RegularExpressions

Public Class BuscarRemplazar

    Private Structure textMatch
        Dim startIndex As Integer
        Dim length As Integer
    End Structure

    Dim startAt As Integer = 0
    Dim lastMatch As textMatch = Nothing

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        findMatch(0)
        btnFindNext.Enabled = True
    End Sub

    Private Sub btnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindNext.Click
        btnFindNext.Enabled = findMatch(startAt)
    End Sub

    Private Function findMatch(ByVal startindex As Integer, Optional ByVal displayMSG As Boolean = True) As Boolean
        Dim findString As Regex = Nothing

        If txtFind.Text.Trim <> String.Empty Then
            If chkWholeWord.Checked And Not chkMatchCase.Checked Then
                findString = New Regex(String.Format("{0}{1}{0}", "\b", txtFind.Text), RegexOptions.IgnoreCase)
            ElseIf chkWholeWord.Checked And chkMatchCase.Checked Then
                findString = New Regex(String.Format("{0}{1}{0}", "\b", txtFind.Text))
            ElseIf Not chkWholeWord.Checked And Not chkMatchCase.Checked Then
                findString = New Regex(txtFind.Text, RegexOptions.IgnoreCase)
            ElseIf Not chkWholeWord.Checked And chkMatchCase.Checked Then
                findString = New Regex(txtFind.Text)
            End If
        Else
            Exit Function
        End If

        If findString.Match(FormPrincipal.textArea.Text, startindex).Success Then
            FormPrincipal.textArea.SelectionStart = findString.Match(FormPrincipal.textArea.Text, startindex).Index
            FormPrincipal.textArea.SelectionLength = findString.Match(FormPrincipal.textArea.Text, startindex).Length
            lastMatch.startIndex = findString.Match(FormPrincipal.textArea.Text, startindex).Index
            lastMatch.length = findString.Match(FormPrincipal.textArea.Text, startindex).Length
            startAt = lastMatch.startIndex + lastMatch.length
            FormPrincipal.textArea.ScrollToCaret()
            FormPrincipal.textArea.Focus()
            Return True
        Else
            If displayMSG Then MsgBox("Texto no encontrado", MsgBoxStyle.Information)
            startAt = 0
            Return False
        End If
    End Function

    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        If FormPrincipal.textArea.SelectedText <> "" Then
            FormPrincipal.textArea.SelectedText = txtReplaceWith.Text
        Else
            FormPrincipal.textArea.SelectionStart = lastMatch.startIndex
            FormPrincipal.textArea.SelectionLength = lastMatch.length
            FormPrincipal.textArea.SelectedText = txtReplaceWith.Text
        End If
    End Sub

    Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceAll.Click

        Dim counter As Integer = 0

        startAt = 0
        Do
            If findMatch(startAt, False) Then
                FormPrincipal.textArea.SelectedText = txtReplaceWith.Text
                counter += 1
            Else
                Exit Do
            End If
        Loop

        If counter > 0 Then
            MsgBox(counter & " palabras reemplazadas", MsgBoxStyle.Information)
        Else
            MsgBox("Texto no encontrado", MsgBoxStyle.Information)
        End If

        startAt = 0

    End Sub

End Class